﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: FZH
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Code;
using FriendStar.Domain.Entity.WareHouse;
using FriendStar.Domain.IRepository.WareHouse;
using FriendStar.Repository.WareHouse;

namespace FriendStar.Application.WareHouse
{
    /// <summary>
	/// 数据视图 V_WWK_47_MX_StoCPKListAcco_Arc 对应的数据应用功能类。
	/// </summary>
	public class V_WWK_47_MX_StoCPKListAcco_ArcApp
    {       
        private string UseUnit = Configs.GetValue("UseUnit");  

        private IV_WWK_47_MX_StoCPKListAcco_ArcRepository service = new V_WWK_47_MX_StoCPKListAcco_ArcRepository();

        public List<V_WWK_47_MX_StoCPKListAcco_ArcEntity> GetList(string keyword)
        {
            var expression = ExtLinq.True<V_WWK_47_MX_StoCPKListAcco_ArcEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.FDocuListID == keyword);
            }
            return service.FindList(expression);
        }

        public List<V_WWK_47_MX_StoCPKListAcco_ArcEntity> GetList(Pagination pagination, string[] FDocuTypeNOs, string FAccountDate, string FDocuNO, string FHappenComNO, string FProductionNO, string FStoreNO, string FCurDate, string FCredence)
        {
            var expression = ExtLinq.True<V_WWK_47_MX_StoCPKListAcco_ArcEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.FAccountDate == FAccountDate);
            if (!string.IsNullOrEmpty(FDocuNO))
            {
                expression = expression.And(t => t.FDocuNO.Contains(FDocuNO));
            }
            if (!string.IsNullOrEmpty(FHappenComNO))
            {
                expression = expression.And(t => t.FHappenComNO == FHappenComNO);
            }
            if (!string.IsNullOrEmpty(FProductionNO))
            {
                expression = expression.And(t => t.FProductionNO.Contains(FProductionNO));
            }
            if (!string.IsNullOrEmpty(FStoreNO))
            {
                expression = expression.And(t => t.FStoreNO == FStoreNO);
            }
            if (!string.IsNullOrEmpty(FCurDate))
            {
                expression = expression.And(t => t.FCurDate == FCurDate);
            }
            if (!string.IsNullOrEmpty(FCredence))
            {
                expression = expression.And(t => t.FCredence.Contains(FCredence));
            }
            string extWhere = "";
            if (FDocuTypeNOs != null && FDocuTypeNOs.Length > 0)
            {
                extWhere += "(";
                for (int i = 0; i < FDocuTypeNOs.Length; i++)
                {
                    if (i == 0)
                    {
                        extWhere += "FDocuTypeNO='" + FDocuTypeNOs[i] + "'";
                    }
                    else
                    {
                        extWhere += " OR FDocuTypeNO='" + FDocuTypeNOs[i] + "'";
                    }
                }
                extWhere += ")";
            }
            return service.FindList(expression, pagination, extWhere);
        }


        public List<V_WWK_47_MX_StoCPKListAcco_ArcEntity> GetListByDocuNO(string FDocuNO)
        {
            var expression = ExtLinq.True<V_WWK_47_MX_StoCPKListAcco_ArcEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.FDocuNO == FDocuNO);
            return service.IQueryable(expression).OrderBy(t => t.FCreateDate, OrderByType.Asc).OrderBy(t => t.FCreateTime, OrderByType.Asc).ToList();
        }

        
        public V_WWK_47_MX_StoCPKListAcco_ArcEntity GetItemByDocuNO(string FDocuNO)
        {
            var expression = ExtLinq.True<V_WWK_47_MX_StoCPKListAcco_ArcEntity>();
            expression = expression.And(t => t.FDocuNO == FDocuNO);
            var list = service.IQueryable(expression).OrderBy(t => t.FCreateDate, OrderByType.Asc).OrderBy(t => t.FCreateTime, OrderByType.Asc).ToList();
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        public V_WWK_47_MX_StoCPKListAcco_ArcEntity GetForm(string keyValue)
        {
            return service.FindEntity(t => t.FDocuListID == keyValue);
        }
    }
}